package demo2;

/**
 * @Author liangzai
 * @Description:
 */
class SingletonLazy{
    private static SingletonLazy instance = null;
    private Object locker = null;

    public synchronized static SingletonLazy getInstance() throws InterruptedException {
        if (instance == null) {
            Thread.sleep(1000);
            instance = new SingletonLazy();
        }
        return instance;
    }

    private SingletonLazy(){

    }
}

public class Test {
    public static SingletonLazy single1 = null;
    public static SingletonLazy single2 = null;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            try {
                single1 = SingletonLazy.getInstance();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread t2 = new Thread(()->{
            try {
                single2 = SingletonLazy.getInstance();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(single1 == single2);
    }
}
